//
//  Message.m
//  QuantumWar
//
//  Created by mysj on 16/7/13.
//  Copyright © 2016年 zeng. All rights reserved.
//

#import "Message.h"
#import "NSString+Add.h"
#import "UIView+Add.h"

@implementation Message


- (void)setM_userName:(NSString *)m_userName {
    if (m_userName) {
        _m_userName = [NSString stringWithFormat:@"%@: ",m_userName];
    }
}

- (void)setM_Text:(NSString *)m_Text {
    _m_Text = m_Text;
    
    [self.text beginEditing];
    
    {
        NSMutableAttributedString *userName = [[NSMutableAttributedString alloc] initWithString:self.m_userName];
        
        if (self.isUser) {
            [userName addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#00bdff"] range:NSMakeRange(0, self.m_userName.length)];
        } else {
            [userName addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#02fe51"] range:NSMakeRange(0, self.m_userName.length)];
        }
        [_text appendAttributedString:userName];
    }
    
    
    
    NSArray *arraymy = [self.m_Text getRanges:@"[" andTow:@"]"];
    
    
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:self.m_Text];
    NSMutableArray *atArray = @[].mutableCopy;
    for (int i = 0; i < arraymy.count; i++) {
        NSRange r;
        [arraymy[i] getValue:&r];
        [one addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:r];
        [one addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#f5ff00"] range:r];
        
        [atArray addObject:[one.string substringWithRange:r]];
    }
    _atUser = atArray.copy;
    
    [_text appendAttributedString:one];
    
    [_text endEditing];
    
//    CGFloat width = [UIScreen mainScreen].bounds.size.width - [self.m_time widthForFont:[UIFont systemFontOfSize:12]] - 10.5f;
    
    CGSize size = [_text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    //  误差
    CGFloat H = size.height - (((int)size.height % 13) + 1) * 2.0f;
    self.cellH = H + 10;

}

- (NSMutableAttributedString *)text {
    if (!_text) {
        _text = [NSMutableAttributedString new];
    }
    return _text;
}

@end
